package com.ctf.soft.ms.controls.manage;

import com.ctf.soft.ms.dao.dto.manage.Pager;
import com.ctf.soft.ms.dao.dto.manage.Relation;
import com.ctf.soft.ms.dao.model.manage.Group;
import com.ctf.soft.ms.dao.model.manage.GroupRelation;
import com.ctf.soft.ms.service.manage.IGroupService;
import com.ctf.soft.ms.utils.AnnotationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.Column;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 名称： GroupController<br>
 * 描述： 群组控制器<br>
 * 最近修改时间：2015/7/31 13:33<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Controller
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private IGroupService groupService;

    @RequestMapping("/view")
    public String doView() {
        return "manage/group/view";
    }

    @RequestMapping("/editor")
    public String doEditor() {
        return "manage/group/editor";
    }

    @RequestMapping("/SaveOrUpdate")
    @ResponseBody
    public String doSaveOrUpdate(@RequestBody ModelMap map,
                                 HttpServletRequest request) {
        String result = "faild";
        try {
            Relation<Group> relation = new Relation<Group>();
            relation.setRelationCode(map.get("relationCode").toString().trim());
            relation.setRelationName(map.get("relationName").toString().trim());
            Group group = new Group();
            group.setId(map.get("id").toString().trim());
            group.setGroupName(map.get("groupName").toString().trim());
            group.setStatus(map.get("status").toString().trim());
            group.setDescription(map.get("description").toString().trim());
            relation.setT(group);
            int c = groupService.saveOrUpdate(relation);
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Delete")
    @ResponseBody
    public String doDelete(@RequestParam("id") String id,
                           HttpServletRequest request) {
        String result = "faild";
        try {
            int c = -1;
            Group group = new Group();
            group.setId(id);
            group.setStatus("D");
            c = groupService.updateByPrimaryKeySelective(group);
            if (c > 0) {
                result = "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/Data")
    @ResponseBody
    public Group loadData(@RequestParam("id") String id,
                          HttpServletRequest request) {
        Group group = null;
        try {
            group = groupService.selectByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return group;
    }

    @RequestMapping("/List")
    @ResponseBody
    public List<Group> loadList(HttpServletRequest request) {
        List<Group> list = null;
        try {
            Group params = new Group();
            params.setStatus("Y");
            list = groupService.selectByList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @RequestMapping("/PageList")
    @ResponseBody
    public Pager<Group> loadPageList(@RequestBody ModelMap params,
                                     HttpServletRequest request) {
        Pager<Group> pager = null;
        try {
            params.put("status", "D");
            String sidx = (String) params.get("sidx");
            String sord = (String) params.get("sord");
            params.put("sord", sord);
            params.put("sidx", AnnotationUtil.getColumnValue(Group.class, Column.class, sidx, "name"));

            int totalRecord = groupService.getPageCount(params);
            int page = Integer.valueOf(params.get("page").toString());
            int rows = Integer.valueOf(params.get("rows").toString());
            int totalPage = totalRecord % rows == 0 ? totalRecord / rows
                    : totalRecord / rows + 1;
            List<Group> list = groupService.loadPageList(params);
            if (list != null) {
                pager = new Pager<Group>();
                pager.setCurrPage(page);
                pager.setTotalPage(totalPage);
                pager.setTotalRecord(totalRecord);
                pager.setList(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pager;
    }

    @RequestMapping("/Relation")
    @ResponseBody
    public List<GroupRelation> loadRelation(
            @RequestParam("id") String defaultGroup, HttpServletRequest request) {
        List<GroupRelation> list = null;
        try {
            list = groupService.loadListByGroup(defaultGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
